package com.xbh.management.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xbh.common.web.core.Result;
import com.xbh.management.model.ManLeave;
import com.xbh.management.service.ManLeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: management-center
 * @description:
 * @author: 许宝华
 * @create: 2022-03-07 10:29
 */

@RestController
@RequestMapping(path = "/leave")
public class ManLeaveController {

    @Autowired
    private ManLeaveService manLeaveService;

    @PostMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    public Result save(@RequestBody JSONObject params){

        if(StrUtil.isEmpty(params.getString("startTime"))){
            return Result.failedParams("开始时间不能为空");
        }
        if(StrUtil.isEmpty(params.getString("endTime"))){
            return Result.failedParams("结束不能为空");
        }

        boolean flag = manLeaveService.saveLeave(params);
        if(flag){
            return Result.success("保存成功");
        }
        return Result.failed("保存失败");
    }

    @PostMapping("/page")
    public Result page(@RequestBody JSONObject params){
        ManLeave manLeave = params.toJavaObject(ManLeave.class);
        int pageNum = params.getInteger("pageNum");
        int pageSize = params.getInteger("pageSize");
        IPage<ManLeave> list = manLeaveService.list(new Page<ManLeave>(pageNum, pageSize), manLeave);
        return Result.success(list);
    }
}
